/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package com.aionemu.gameserver.model.gameobjects.stats.modifiers;

import com.aionemu.gameserver.model.gameobjects.Item;
import com.aionemu.gameserver.model.gameobjects.stats.Stat;
import com.aionemu.gameserver.model.gameobjects.stats.StatEnum;
import com.aionemu.gameserver.model.items.ItemSlot;

/**
 * @author Mr. Poke
 *
 */
public class EnchantModifier extends StatModifier
{

	private Item	owner;

	/**
	 * @param owner
	 */
	public EnchantModifier(Item owner, StatEnum stat)
	{
		super();
		this.priority = 0x20;
		this.setStat(stat);
		this.owner = owner;
	}

	/* (non-Javadoc)
	 * @see com.aionemu.gameserver.model.gameobjects.stats.modifiers.StatModifier#apply(com.aionemu.gameserver.model.gameobjects.stats.Stat)
	 */
	@Override
	public void apply(Stat stat)
	{
		if (!owner.isEquipped())
			return ;
		int enchantLvl = owner.getEnchantLevel();
		if (enchantLvl == 0)
			return ;
		if (owner.getEquipmentSlot() == ItemSlot.MAIN_SUB_HAND.getSlotIdMask() || owner.getEquipmentSlot() == ItemSlot.OFF_SUB_HAND.getSlotIdMask())
			return;
		stat.base+= getEnchantAdditionModifier(owner.getEnchantLevel());
	}

	private int getEnchantAdditionModifier(int enchantLvl)
	{
		if (owner.getItemTemplate().isWeapon())
		{
			switch (this.getStat())
			{
				case PHYSICAL_ATTACK_MAIN_HAND:
				case PHYSICAL_ATTACK_OFF_HAND:
				case PHYSICAL_ATTACK:
					switch (owner.getItemTemplate().getWeaponType())
					{
						case DAGGER_1H:
						case SWORD_1H:
							return (2 * enchantLvl);
						case POLEARM_2H:
						case SWORD_2H:
						case BOW:
							return (4 * enchantLvl);
						case MACE_1H:
						case STAFF_2H:
							return (3 * enchantLvl);
					}
					return 0;
				case BOOST_MAGICAL_SKILL:
					switch (owner.getItemTemplate().getWeaponType())
					{
						case BOOK_2H:
						case MACE_1H:
						case STAFF_2H:
						case ORB_2H:
							return (20 * enchantLvl);
					}
					return 0;
				case MAGICAL_ATTACK:
					switch (owner.getItemTemplate().getWeaponType())
					{
						case BOOK_2H:
						case ORB_2H:
							return (3 * enchantLvl);
					}
					return 0;
			}
		}
		else if (owner.getItemTemplate().isArmor())
		{
			switch (owner.getItemTemplate().getArmorType())
			{
				case ROBE :
					switch(owner.getEquipmentSlot())
					{
						case 1 << 5:
						case 1 << 11:
						case 1 << 4:
							switch (this.getStat())
							{
								case PHYSICAL_DEFENSE:
									return  enchantLvl;
								case MAXHP:
									return  (10*enchantLvl);
								case PHYSICAL_CRITICAL_RESIST:
									return  (-2*enchantLvl);
							}
						return 0;
						case 1 << 12:
							switch (this.getStat())
							{
								case PHYSICAL_DEFENSE:
									return (2*enchantLvl);
								case MAXHP:
									return  (12*enchantLvl);
								case PHYSICAL_CRITICAL_RESIST:
									return (-3*enchantLvl);
							}
						return 0;
						case 1 << 3:
							switch (this.getStat())
							{
								case PHYSICAL_DEFENSE:
									return  (3*enchantLvl);
								case MAXHP:
									return  (14*enchantLvl);
								case PHYSICAL_CRITICAL_RESIST:
									return  (-4*enchantLvl);
							}
						return 0;
					}
					return 0;
				case LEATHER :
					switch(owner.getEquipmentSlot())
					{
						case 1 << 5:
						case 1 << 11:
						case 1 << 4:
							switch (this.getStat())
							{
								case PHYSICAL_DEFENSE:
									return (3*enchantLvl);
								case MAXHP:
									return (8*enchantLvl);
								case PHYSICAL_CRITICAL_RESIST:
									return (-2*enchantLvl);
							}
						return 0;
						case 1 << 12:
							switch (this.getStat())
							{
								case PHYSICAL_DEFENSE:
									return (5*enchantLvl);
								case MAXHP:
									return (10*enchantLvl);
								case PHYSICAL_CRITICAL_RESIST:
									return (-3*enchantLvl);
							}
						return 0;
						case 1 << 3:
							switch (this.getStat())
							{
								case PHYSICAL_DEFENSE:
									return (4*enchantLvl);
								case MAXHP:
									return (12*enchantLvl);
								case PHYSICAL_CRITICAL_RESIST:
									return (-4*enchantLvl);
							}
						return 0;
					}
					return 0;
				case CHAIN :
					switch(owner.getEquipmentSlot())
					{
						case 1 << 5:
						case 1 << 11:
						case 1 << 4:
							switch (this.getStat())
							{
								case PHYSICAL_DEFENSE:
									return (3*enchantLvl);
								case MAXHP:
									return (6*enchantLvl);
								case PHYSICAL_CRITICAL_RESIST:
									return (-2*enchantLvl);
							}
						return 0;
						case 1 << 12:
							switch (this.getStat())
							{
								case PHYSICAL_DEFENSE:
									return (5*enchantLvl);
								case MAXHP:
									return (10*enchantLvl);
								case PHYSICAL_CRITICAL_RESIST:
									return (-3*enchantLvl);
							}
						return 0;
						case 1 << 3:
							switch (this.getStat())
							{
								case PHYSICAL_DEFENSE:
									return (5*enchantLvl);
								case MAXHP:
									return (10*enchantLvl);
								case PHYSICAL_CRITICAL_RESIST:
									return (-4*enchantLvl);
							}
						return 0;
					}
					return 0;
				case PLATE :
					switch(owner.getEquipmentSlot())
					{
						case 1 << 5:
						case 1 << 11:
						case 1 << 4:
							switch (this.getStat())
							{
								case PHYSICAL_DEFENSE:
									return (4*enchantLvl);
								case MAXHP:
									return (4*enchantLvl);
								case PHYSICAL_CRITICAL_RESIST:
									return (-2*enchantLvl);
							}
						return 0;
						case 1 << 12:
							switch (this.getStat())
							{
								case PHYSICAL_DEFENSE:
									return (5*enchantLvl);
								case MAXHP:
									return (6*enchantLvl);
								case PHYSICAL_CRITICAL_RESIST:
									return (3*enchantLvl);
							}
						return 0;
						case 1 << 3:
							switch (this.getStat())
							{
								case PHYSICAL_DEFENSE:
									return (6*enchantLvl);
								case MAXHP:
									return (8*enchantLvl);
								case CRITICAL_RESIST:
									return (-4*enchantLvl);
							}
						return 0;
					}
					return 0;
				case SHIELD:
					switch (this.getStat())
					{
						case DAMAGE_REDUCE:
							if (enchantLvl > 10)
								return -20;
							else
								return (-2*(enchantLvl));
						case BLOCK:
							if (enchantLvl > 10)
								return (30*(enchantLvl-10));
							return 0;
					}
			}
		}
		return 0;
	}

}
